﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Beauty.BLL;
using Beauty.Model;
using WebUI.Models;
namespace WebUI.Controllers
{
    public class ItemsController : Controller
    {
        public int Uid { get; set; }
        public bool IsOver { get; set; }
        // GET: Items
        public ActionResult process(int id)
        {
            UtilWebService util = new UtilWebService();
            #region 首先进行权限判断，不能浏览的话 就没必要下面走了
            //            浏览套图权限判断：
            //获取用户IP，查询数据库是否记录，不存在的话是首次浏览插入记录 ip, 剩余次数，被浏览的套图ID，用户ID(游客统一置零)
            //非首次浏览的话，
            //判断被浏览的套图id只有在不存在的情况下才减少次数,并且记录下套图ID
            string userIP = Request.UserHostAddress;
            album_record record = bllRecord.GetModel(userIP);
            user loginUser = null;
            CommonMethod.CheckLogin(out loginUser);
            if (loginUser == null)
            {
                Uid = 0;
            }
            else
            {
                Uid = loginUser.Uid;
            }
            List<string> aids = new List<string>();
            if (record == null)
            {
                //首次浏览
                record = new album_record();
                record.IP = userIP;
                record.AlbumId = id.ToString();//套图id用逗号分隔存储
                record.UserId = 0;
                record.RemainderNum = int.Parse(System.Configuration.ConfigurationManager.AppSettings["remainder_number"]);
                record.RemainderNum--;
                bllRecord.Add(record);
            }
            else
            {
                //非首次浏览
                string[] albumIds = record.AlbumId.Split(',');//获取已经浏览过的套图id

                foreach (var item in albumIds)
                {
                    aids.Add(item);
                }
                if (record.RemainderNum < 1)
                {
                    IsOver = true;

                }
                else
                {
                    IsOver = false;

                    if (!aids.Contains(id.ToString()))
                    {
                        //记录中不存在被浏览的套图 丧失一次机会
                        record.RemainderNum--;
                        aids.Add(id.ToString());
                        record.AlbumId = string.Join(",", aids.ToArray());
                        bllRecord.Update(record);//更新次数至DB

                    }
                }


            }

            #endregion
            List<picmain> mains;
            List<picalbum> Recommends;
            picalbum album;
            picclass picclass;
            util.GetAlbumDetialData(id, out mains, out Recommends, out picclass, out album);
            if (mains.Count > 0)
            {
               
                List<picclass> classes = bllClass.DataTableToList(bllClass.GetAllList().Tables[0]);
                ViewData["recommends"] = Recommends;//推荐
                ViewData["classes"] = classes;//头部分类

                ViewData["album"] = album;//所属套图
                ViewData["class"] = picclass;//所属分类
                ViewData["mains"] = mains;//套图内图片对象
                ViewData["visted_albumid"] = aids;//访问过的套图记录
                ViewData["user"] = loginUser;
                return View(IsOver);
            }
            else
            {
                return RedirectToAction("_404", "Error");
            }
            ////获取指定套图id的所有图片
            //picmainBLL mainBll = new picmainBLL();
            //List<picmain> mains = mainBll.GetModelList("AlbumId=" + id + " ORDER BY PicId DESC"); ;
            ////获取套图信息
            //if (mains.Count > 0)
            //{
            //    picalbum album = bllAlbum.GetModel((int)mains[0].AlbumId);

            //    //获取套图所在分类
            //    picclass picclass = bllClass.GetModel((int)album.ClassId);

            //    List<picclass> classes = bllClass.DataTableToList(bllClass.GetAllList().Tables[0]);
            //    //获取分类下的4个相关推荐
            //    List<picalbum> Recommends = bllAlbum.GetModelList("ClassId=" + album.ClassId + " AND AlbumId!=" + id + " ORDER BY  RAND() LIMIT 0,4");
            //    foreach (var item in Recommends)
            //    {
            //        picmain pic = bllMain.GetModel((int)item.CoverId);
            //        if (pic == null)
            //        {
            //            //此套图的封面图片对象不存在 设置成默认的
            //            item.CoverSrc = Settings.DefaultImg;
            //        }
            //        else
            //        {
            //            item.CoverSrc = string.Format("../../pic/small/{0}", pic.Src);
            //        }
            //    }

            //    ViewData["recommends"] = Recommends;
            //    ViewData["classes"] = classes;

            //    ViewData["album"] = album;
            //    ViewData["class"] = picclass;
            //    ViewData["mains"] = mains;
            //    ViewData["visted_albumid"] = aids;
            //    ViewData["user"] = loginUser;
            //    return View(IsOver);

            //}
            //else
            //{
            //    return RedirectToAction("_404", "Error");
            //}






        }



        picclassBLL bllClass = new picclassBLL();
        picmainBLL bllMain = new picmainBLL();
        picalbumBLL bllAlbum = new picalbumBLL();
        album_recordBLL bllRecord = new album_recordBLL();

    }
}